﻿#pragma once

#include <QObject>
#include <qhash.h>
#include "ObjAutoInject.h"

#include "LogSaver.h"

class ObjectManager : public QObject, public ObjAutoInject
{
	Q_OBJECT
private://property
	QHash<QString, QObject*> objMap;//对象映射
	static ObjectManager* objectManager;//全局唯一ObjectManager对象
private://dependency
	LogSaver* logSaver = LogSaver::getLogSaver();//日志对象
public:
	explicit ObjectManager(void);
	virtual ~ObjectManager(void);
	static ObjectManager* getObjectManager();//从该函数可以获得全局唯一ObjectManager对象
signals:
private:
	void objectCreation();//对象创建
	void objectInjection();//对象注入
	void objectInitialization();//对象初始化
	template<class A>void addToObjMap(A* a, QString objName = QString())
	{
		addToObjMap((QObject*)a, objName.isEmpty() ? getClassNameFirstLow<A>() : objName);
	}
	template<class A>void addToObjMap(QString objName = QString())
	{
		addToObjMap<A>(new A(), objName);
	}
	template<class A>A* getObjFromMap()
	{
		return ObjAutoInject::getObjFromMap<A>(&objMap);
	}
	template<class A>bool isMapContains()
	{
		return objMap.contains(getClassNameFirstLow<A>());
	}
	void removeFromObjMap(QObject* obj);
	void removeFromObjMap(QString objName);
	void addToObjMap(QObject* obj, QString objName);
};